package com.syt.cellphone.information.http;

import android.content.Context;
import android.content.res.Resources;
import com.syt.cellphone.information.App;
import com.syt.cellphone.information.R;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.text.ParseException;

import cn.hutool.http.HttpException;

/**
 * 异常处理
 */
public class RxExceptionUtil {

    public static String exceptionHandler(Throwable e){
        Context context = App.Companion.getINSTANCE();
        Resources resources = context.getResources();
        String errorMsg = resources.getString(R.string.requet_error_unknow);
        if (e instanceof UnknownHostException) {
            errorMsg =resources.getString(R.string.requet_error_unknowhost);
        } else if (e instanceof SocketTimeoutException) {
            errorMsg = resources.getString(R.string.requet_error_timeout);
        } else if (e instanceof ConnectException) {
            ConnectException connectException = (ConnectException) e;
            errorMsg = resources.getString(R.string.requet_error_failed_to_connect);
        } else if (e instanceof ParseException || e instanceof JSONException
                || e instanceof JSONException) {
            errorMsg =  resources.getString(R.string.requet_error_jsonerror);
        }
        return errorMsg;
    }
}
